package ua.crimea.bat.ss.model.beans.users;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import ua.crimea.bat.ss.model.beans.BaseBean;

import javax.persistence.*;
import java.util.Collection;

/**
 * <p/>
 * <p/>
 * Original Author:   <a href="mailto:Alexey Batyuta <alexey@pilgrimcs.com.ua>">Alexey Batyuta</a><br/>
 * Created Date:     Oct 11, 2010 <br/>
 * Created Time:     3:59:46 PM <br/>
 * <p/>
 * $Id: User.java 59 2010-10-12 08:29:50Z alexey.batyuta@gmail.com $
 * <p/>
 * $Author: alexey.batyuta@gmail.com $
 * $Date: 2010-10-12 08:29:50 +0000 (Tue, 12 Oct 2010) $
 * $Rev: 59 $
 * $URL: http://bat-test-docs.googlecode.com/svn/tags/releases/spring_security-0.0.1/core/src/main/java/ua/crimea/bat/ss/model/beans/users/User.java $
 */

@Entity
@Table(name = "users")
public class User extends BaseBean implements UserDetails {
    private String username;
    private String password;
    private Boolean enabled;
    private Boolean accountNonExpired;
    private Boolean credentialsNonExpired;
    private Boolean accountNonLocked;
    private Collection<GrantedAuthority> authorities;

    @OneToMany(targetEntity= UserAuthority.class, cascade = {javax.persistence.CascadeType.REMOVE},fetch = FetchType.EAGER, mappedBy = "userName")
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    private String deptName;

    /**
     * @return Returns the username.
     */
    @Column(name = "username", length = 100)
    public String getUsername() {
        return username;
    }


    @Column(name = "account_non_expired")
    public Boolean getAccountNonExpired(){
        return accountNonExpired;
    }

    @Override
    @Transient
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }


    @Column(name = "account_non_locked")
    public Boolean getAccountNonLocked() {
        return accountNonLocked;
    }
    @Override
    @Transient
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Column(name = "credentials_non_expired")
    public Boolean getCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    @Transient
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Column(name = "enabled")
    public Boolean getEnabled() {
        return enabled;
    }

    @Override
    @Transient
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * @param username The username to set.
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return Returns the deptName.
     */
    @Column(name = "dept_name", length = 100)
    public String getDeptName() {
        return deptName;
    }

    /**
     * @param deptName The deptName to set.
     */
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    @Column(name = "password", length = 512)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setAccountNonExpired(Boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public void setAccountNonLocked(Boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }
}